﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using TRicheControl.Comm;

namespace TRicheControl.WinformControl
{
    [ToolboxBitmap(typeof(VScrollBar))]
    public partial class RicheVScrollBar : Control
    {
        #region Property

        private int mAllDisplayHeight = 0;
        /// <summary>
        /// 所有区域高度
        /// </summary>
        [Description("所有区域高度")]
        public int AllDisplayHeight
        {
            get
            {
                return mAllDisplayHeight - CurrentDisplayHeight;
            }
            set
            {
                mAllDisplayHeight = value;
                ResetScrollBarRectangle();
            }
        }

        private int mCurrentLocation = 0;
        /// <summary>
        /// 当前滚动条位置
        /// </summary>
        private int CurrentLocation 
        {
            get
            {
                return mCurrentLocation;
            }
            set
            {
                if (value < 0)
                {
                    mCurrentLocation = 0;
                }
                else if (value + mSliderRect.Height > mSliderAreaRect.Height)
                {
                    mCurrentLocation = mSliderAreaRect.Height - mSliderRect.Height;
                }
                else
                {
                    mCurrentLocation = value;
                }
                ResetScrollBarRectangle();
                if (mOnScroll != null)
                {
                    mOnScroll(this, new EventArgs());
                }
            }
        }

        [Browsable(true), Description("当前滚动条位置")]
        public int Value
        {
            get
            {
                return (int)(((CurrentLocation + 0.00) / ((mSliderAreaRect.Height - mSliderRect.Height) + 0.00)) * AllDisplayHeight);
            }
            set
            {
                int i = value;
                if (i < 0)
                {
                    i = 0;
                }
                else if (i > AllDisplayHeight)
                {
                    i = AllDisplayHeight;
                }
                CurrentLocation = (int)((((mSliderAreaRect.Height - mSliderRect.Height) + 0.00) / AllDisplayHeight) * i);
            }
        }

        private int mCurrentDisplayHeight = 0;
        /// <summary>
        /// 当前显示的区域高度
        /// </summary>
        [Description("当前显示的区域高度")]
        public int CurrentDisplayHeight
        {
            get
            {
                return mCurrentDisplayHeight;
            }
            set
            {
                mCurrentDisplayHeight = value;
                ResetScrollBarRectangle();
            }
        }

        private int mScrollLenght = 5;
        /// <summary>
        /// 每次滚动的高度
        /// </summary>
        [Description("每次滚动的高度")]
        public int ScrollLenght
        {
            get
            {
                return mScrollLenght;
            }
            set
            {
                mScrollLenght = value;
            }
        }

        private int mMinSliderHeight = 20;

        private int mSliderHeight = 20;
        /// <summary>
        /// 滑块的高度
        /// </summary>
        [Description("滑块的高度")]
        public int SilderHeight
        {
            get
            {
                if (AllDisplayHeight != 0 && CurrentDisplayHeight != 0)
                {
                    mSliderHeight = (int)(((CurrentDisplayHeight + 0.00) / (AllDisplayHeight + 0.00)) * mSliderAreaRect.Height);
                }
                if (mSliderHeight < mMinSliderHeight)
                {
                    mSliderHeight = mMinSliderHeight;
                }
                return mSliderHeight;
            }
            set
            {
                mSliderHeight = value;
                ResetScrollBarRectangle();
            }
        }
        private int mWidth = 20;
        public int Width
        {
            get
            {
                return mWidth;
            }
            set
            {
                mWidth = value;
            }
        }
        private Graphics mGraphics;
        public Graphics Graphics
        {
            get
            {
                if (base.IsDisposed)
                {
                    return null;
                }
                if (mGraphics == null)
                {
                    mGraphics = base.CreateGraphics();
                }
                return mGraphics;
            }
        }

        #endregion

        #region Delegates


        #endregion

        #region Actions

        private event EventHandler mOnScroll;
        /// <summary>
        /// 滚动条滚动事件
        /// </summary>
        [Description("滚动条滚动事件")]
        public event EventHandler OnScroll
        {
            add { mOnScroll += value; }
            remove { mOnScroll -= value; }
        }

        #endregion

        private int mArrowHeight = 20;
        private Rectangle mUpArrowRect;
        private Rectangle mDownArrowRect;
        private Rectangle mSliderAreaRect;
        private Rectangle mSliderRect;

        public RicheVScrollBar()
        {
            InitializeComponent();
            this.Dock = DockStyle.Right;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                          ControlStyles.ResizeRedraw |
                          ControlStyles.AllPaintingInWmPaint, true);
            mUpArrowRect = new Rectangle(0, 0, this.Width, mArrowHeight);
            this.Width = 20;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            ResetScrollBarRectangle();
        }

        private void ResetScrollBarRectangle()
        {
            mDownArrowRect = new Rectangle(0, this.Height - mArrowHeight, this.Width, mArrowHeight);
            mSliderAreaRect = new Rectangle(0, mUpArrowRect.Y + mUpArrowRect.Height + 1, this.Width, this.Height - (mUpArrowRect.Height + mDownArrowRect.Height) - 2);
            mSliderRect = new Rectangle(0, mSliderAreaRect.Y + CurrentLocation, this.Width, SilderHeight);
            this.Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            GraphicsStyle.Styles.VScrollBarStyle.DrawBarBackGround(e.Graphics, new Rectangle(0, 0, this.Width, this.Height));
            GraphicsStyle.Styles.VScrollBarStyle.DrawUpArrow(e.Graphics, mUpArrowRect);
            GraphicsStyle.Styles.VScrollBarStyle.DrawDownArrow(e.Graphics, mDownArrowRect);
            if (mSliderRect != null)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawSlider(e.Graphics, mSliderRect);
            }
            base.OnPaint(e);
        }

        private bool mIsMouseDown = false;
        private Point mMouseDownLocation = new Point();
        private Point mMouseLocation = new Point();

        protected override void OnMouseDown(MouseEventArgs e)
        {
            ScrollActivateObjectType type = GetActivateObjectType(e.Location);
            if (e.Button == System.Windows.Forms.MouseButtons.Left && type == ScrollActivateObjectType.Silder)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawMouseDownSlider(Graphics, mSliderRect);
                mIsMouseDown = true;
                mMouseDownLocation = e.Location;
            }
            else if (type == ScrollActivateObjectType.UpButton)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawMouseDownUpArrow(Graphics, mUpArrowRect);
                Value -= ScrollLenght;
            }
            else if (type == ScrollActivateObjectType.DownButton)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawMouseDownDownArrow(Graphics, mDownArrowRect);
                Value += ScrollLenght;
            }
            else if (type == ScrollActivateObjectType.ScrollArea)
            {
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            ScrollActivateObjectType type = GetActivateObjectType(e.Location);
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                mIsMouseDown = false;
            }
            if (type == ScrollActivateObjectType.Silder)
            {
                
            }
            else if (type == ScrollActivateObjectType.UpButton)
            {

            }
            else if (type == ScrollActivateObjectType.DownButton)
            {
            }
            else if (type == ScrollActivateObjectType.ScrollArea)
            {
            }
            
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (mIsMouseDown == true && e.Y >= mSliderRect.Y && e.Y <= mSliderRect.Y + mSliderRect.Height)
            {
                CurrentLocation += e.Y - mMouseDownLocation.Y;
                mMouseDownLocation = e.Location;
            }
            mMouseLocation = e.Location;
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            ScrollActivateObjectType type = GetActivateObjectType(e.Location);
            if (type == ScrollActivateObjectType.Silder)
            {
            }
            else if (type == ScrollActivateObjectType.UpButton)
            {
                
            }
            else if (type == ScrollActivateObjectType.DownButton)
            {
                
            }
            else if (type == ScrollActivateObjectType.ScrollArea)
            {
            }
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            //CurrentLocation -= e.Delta;
            if (e.Delta > 0)
            {
                Value -= ScrollLenght;
            }
            else if (e.Delta < 0) 
            {
                Value += ScrollLenght;
            }
            base.OnMouseWheel(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            ScrollActivateObjectType type = GetActivateObjectType(mMouseLocation);
            if (type == ScrollActivateObjectType.Silder)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawMouseEnterSlider(Graphics, mSliderRect);
            }
            else if (type == ScrollActivateObjectType.UpButton)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawMouseEnterUpArrow(Graphics, mUpArrowRect);
            }
            else if (type == ScrollActivateObjectType.DownButton)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawMouseEnterDownArrow(Graphics, mDownArrowRect);
            }
            else if (type == ScrollActivateObjectType.ScrollArea)
            {
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            ScrollActivateObjectType type = GetActivateObjectType(mMouseLocation);
            if (type == ScrollActivateObjectType.Silder)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawSlider(Graphics, mSliderRect);
            }
            else if (type == ScrollActivateObjectType.UpButton)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawUpArrow(Graphics, mUpArrowRect);
            }
            else if (type == ScrollActivateObjectType.DownButton)
            {
                GraphicsStyle.Styles.VScrollBarStyle.DrawDownArrow(Graphics, mDownArrowRect);
            }
            else if (type == ScrollActivateObjectType.ScrollArea)
            {
            }
        }

        private ScrollActivateObjectType GetActivateObjectType(Point mouseLocation)
        {
            if (ControlHelper.PointInArea(mouseLocation, mUpArrowRect))
            {
                return ScrollActivateObjectType.UpButton;
            }
            else if (ControlHelper.PointInArea(mouseLocation, mDownArrowRect))
            {
                return ScrollActivateObjectType.DownButton;
            }
            else if(ControlHelper.PointInArea(mouseLocation, mSliderRect))
            {
                return ScrollActivateObjectType.Silder;
            }
            else if (ControlHelper.PointInArea(mouseLocation, mSliderAreaRect))
            {
                return ScrollActivateObjectType.ScrollArea;
            }
            return ScrollActivateObjectType.None;
        }
    }
}
